import 'package:message_tools/db/database.dart';
import 'package:message_tools/model/sms_send_record.dart';
import 'package:message_tools/model/sms_task.dart';
import 'package:message_tools/model/sms_template.dart';

class DbRepository {
  static Future<List> fetchSmsTemplateList() async {
    Database database = await Db().getDatabase();
    var data =
        await database.rawQuery('SELECT * FROM sms_template order by id desc');

    return data
        .map<SmsTemplate>((item) => SmsTemplate.fromJsonMap(item))
        .toList();
  }

  static insertSmsTemplate(SmsTemplate smsTemplate) async {
    Database database = await Db().getDatabase();
    database.execute('''
        insert into sms_template(title, content, created_at) 
        values('${smsTemplate.title}', '${smsTemplate.content}', '${smsTemplate.createdAt}')
    ''');
  }

  static updateSmsTemplate(SmsTemplate smsTemplate) async {
    Database database = await Db().getDatabase();
    database.execute('''
        update sms_template set title = '${smsTemplate.title}', 
        content = '${smsTemplate.content}', created_at = '${smsTemplate.createdAt}' where id = '${smsTemplate.id}'
    ''');
  }

  void deleteSmsTemplateById(int id) async {
    Database database = await Db().getDatabase();
    database.rawDelete('DELETE FROM sms_template WHERE id = ?', [id]);
  }

  static Future<List> fetchSmsTaskList() async {
    Database database = await Db().getDatabase();
    var data =
        await database.rawQuery('SELECT * FROM sms_task order by id desc');

    return data.map<SmsTask>((item) => SmsTask.fromJsonMap(item)).toList();
  }

  static insertSmsTask(SmsTask smsTask) async {
    Database database = await Db().getDatabase();
    database.execute('''
        insert into sms_task(title, sms_template_title, sms_template_content, contact_file_path, created_at) 
        values('${smsTask.title}', '${smsTask.smsTemplateTitle}', 
        '${smsTask.smsTemplateContent}', '${smsTask.contactFilePath}', '${smsTask.createdAt}')
    ''');
  }

  static updateSmsTask(SmsTask smsTask) async {
    Database database = await Db().getDatabase();
    database.execute('''
        update sms_task set title = '${smsTask.title}', 
        sms_template_title = '${smsTask.smsTemplateTitle}',
        sms_template_content = '${smsTask.smsTemplateContent}',
        contact_file_path = '${smsTask.contactFilePath}' where id = '${smsTask.id}'
    ''');
  }

  void deleteSmsTaskById(int id) async {
    Database database = await Db().getDatabase();
    database.rawDelete('DELETE FROM sms_task WHERE id = ?', [id]);
  }

  static Future<List<SmsSendRecord>> fetchSmsSendRecordList(
      int status, int offset, int limit) async {
    Database database = await Db().getDatabase();
    var data = await database.rawQuery(
        'SELECT * FROM sms_send_record where status = $status limit $limit offset $offset');

    return data
        .map<SmsSendRecord>((item) => SmsSendRecord.fromJsonMap(item))
        .toList();
  }

  static Future<List<SmsSendRecord>> fetchSmsSentRecordList(
      int offset, int limit) async {
    Database database = await Db().getDatabase();
    var data = await database.rawQuery(
        'SELECT nindex, phone_num FROM sms_send_record where status in (1,2) limit $limit offset $offset');

    return data
        .map<SmsSendRecord>((item) => SmsSendRecord.fromJsonMap(item))
        .toList();
  }

  static Future<List<SmsSendRecord>> fetchSigleSmsSendRecordList(
      int status, int offset, int limit) async {
    Database database = await Db().getDatabase();
    var data = await database.rawQuery(
        'SELECT nindex, phone_num FROM sms_send_record where status = $status limit $limit offset $offset');

    return data
        .map<SmsSendRecord>((item) => SmsSendRecord.fromJsonMap(item))
        .toList();
  }

  static insertSmsRecord(String index, String phoneNum, String content,
      int smsTaskId, int status) async {
    Database database = await Db().getDatabase();
    database.execute('''
        insert into sms_send_record(nindex, phone_num, content, sms_task_id, status) 
        values('$index', '$phoneNum', '$content', '$smsTaskId', $status)
    ''');
  }

  static updateSmsRecordStatus(String phoneNum, int status) async {
    Database database = await Db().getDatabase();
    database.execute('''
        update sms_send_record set status = ${status}
        where phone_num = '$phoneNum'
    ''');
  }

  static Future<int> getSendCount(int status) async {
    Database database = await Db().getDatabase();
    var count = Sqflite.firstIntValue(await database.rawQuery(
            "select count(*) from sms_send_record where status = $status")) ??
        0;

    return count;
  }

  static deleteSmsRecords() async {
    Database database = await Db().getDatabase();
    database.rawDelete('DELETE FROM sms_send_record');
  }
}
